import { createSlice } from "@reduxjs/toolkit";
import { Admin } from "@/utils/local";
import { Admin_Loing } from '@/apis/admin/admin'
import { message } from "antd"
import type { AppDispatch } from "../index"
import { addrouter } from "@/router"
const AdminStore = createSlice({
    name: "RootStore",
    initialState: {
        AdminInfo: Admin.get_Admin() || {
            pression: [],
            user: {}
        }
    },
    reducers: {
        Login: (state, { payload }) => {

            state.AdminInfo = payload

            Admin.set_Admin(payload)
            addrouter(payload.pression)


        },
        GoOut: (state) => {
            state.AdminInfo = {
                pression: [],
                user: {}
            }
            Admin.remove_Admin()
            addrouter([])
        },

    },
})
export const { Login, GoOut } = AdminStore.actions

const axios_admin_login = (data: any) => {
    return async (dispatch: AppDispatch) => {
        const res = await Admin_Loing(data)
        if (res.status == 200) {
            dispatch(Login(res.data))
            console.log("登录成功")
            message.success("登录成功")
            return true
        } else {
            return false
        }
    }
}
export { axios_admin_login }
export default AdminStore.reducer